import { get, post } from '../../../utils/request.js'
import global from '../../../utils/global.js'
import { getNowFormatDate} from '../../../utils/util.js'
let that
Page({
  data: {
    array: '请选择体检人',
    index: 0,
    curDay: '请选择体检时间',
    userId: wx.getStorageSync("useInfo").id,
    user:{},
    sellerId:"",
    peId:"",
    dataInfo:{}
  },
  onLoad: function (options) {
    console.log(options,"参数")
    wx.removeStorageSync("selectCurrentDate");
    wx.removeStorageSync("selectItem");
    that = this;
    if(!that.data.userId){
      that.setData({
        userId: options.userId
      }) 
    }
    that.setData({
      productId:options.productId,
    })
  },
  onShow:function(){
    that.getSettleAccounts(that.data.productId);
    that.setData({
      array: wx.getStorageSync("selectItem").name,
      peId: wx.getStorageSync("selectItem").id,
      curDay: wx.getStorageSync("selectCurrentDate")
    })
  },
  getSettleAccounts: (id) => {
    post(global.apiUrl.settleAccounts, { userId: that.data.userId, productId: id }, (res) => {
      if(res.obj){

      }
      that.setData({
        user: res.obj,
        sellerId: res.obj.product[0].sellerId,
        productId: res.obj.product[0].id,
        couponJsonId: res.obj.userCouponJsonId,
        price: res.obj.totalPrices,
        dataInfo: res.obj
      })
    })
  },

 
  // 确认预约
  formSubmit: function (e) {
    let data = e.detail.value;
    data.sellerId = that.data.sellerId;
    data.productId = that.data.productId,
    data.couponJsonId = that.data.couponJsonId
    data.userId = that.data.userId
    data.price = that.data.price
    data.peId = that.data.peId
    data.peDate = that.data.curDay
    
    console.log(data)
    if (!data.contacts) {
        wx.showModal({
          title: '提示',
          content: '等待获取用户基本信息',
        })
        that.getSettleAccounts(that.data.productId);
        return
      }
    if (!data.peId){
        wx.showModal({
        title: '提示',
        content: '请选择体检人',
      })
      return
    }
    console.log(that.data.curDay)
    if (!that.data.curDay) {
      wx.showModal({
        title: '提示',
        content: '请选择体检时间',
      })
      return
    }
    if (!data.checkbox[0]){
      wx.showModal({
        title: '提示',
        content: '请同意授权协议',
      })
      return
    }
    that.data.dataInfo.medicalName = that.data.array;
    that.data.dataInfo.medicalTime = that.data.curDay;
    // 提交预约
    post(global.apiUrl.submitOrder, data, (res) => {
      console.log(res.obj)
      that.data.dataInfo.payId = res.obj.payId;
      console.log(that.data.dataInfo, "343343")
      wx.navigateTo({
        url: '../../pay/confirmPay/confirmPay?dataInfo=' + JSON.stringify(that.data.dataInfo),
      })
    })
    
  },
  bindPickerChange: function () {
    wx.navigateTo({
      url: '/pages/userCenter/regularChecker/regularChecker?select=1',
    })
   
  },
  bindDateChange: function () {
    wx.navigateTo({
      url: '/pages/calendar/calendar?productId=' + that.data.productId,
    })
  },
  userxieyi(){
    wx.navigateTo({
      url: 'userAgreement/userAgreement',
    })
  }
})